﻿using System;
using System.Collections;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static Int32 _intCount = 0;
        static ArrayList _arlDirs = new ArrayList();

        static void Main(string[] args)
        {
            DirectoryInfo diStart = new DirectoryInfo(args[0]);

            _intCount = 0;
            _arlDirs.Clear();
            
            _recurseDirs(diStart);

            Console.WriteLine("Found " + _intCount.ToString() + " dirs to be deleted.");

            if (_intCount > 0)
            {
                Console.WriteLine("Continue with deletion? Press 'y' to continue deletion.");
                ConsoleKeyInfo CKI= Console.ReadKey(true);

                if (CKI.KeyChar.ToString() == "y")
                {
                    Console.WriteLine("Starting deletion. Good luck. :)");
                    _deleteDirs();
                }
                else
                {
                    Console.WriteLine("Not deleting anything. Goodbye. :)");
                }
            }

            Console.ReadKey();
        }

        private static void _recurseDirs(
            DirectoryInfo diWork)
        {
            try
            {
                foreach (DirectoryInfo DI in diWork.EnumerateDirectories())
                {
                    if (DI.FullName.EndsWith(".svn"))
                    {
                        Console.WriteLine("Deleting " + DI.FullName);
                        _arlDirs.Add(DI.FullName);
                        _intCount++;
                    }
                    else
                    {
                        _recurseDirs(DI);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error working on '" + diWork.FullName + "' (" + ex.Message + ")");
            }
        }

        private static void _deleteDirs()
        {
            try
            {
                foreach (String strDir in _arlDirs)
                {
                    DirectoryInfo diDelete = new DirectoryInfo(strDir);

                    try
                    {
                        Console.WriteLine("Deleting '" + diDelete.FullName + "'");

                        DeleteFileSystemInfo(diDelete as FileSystemInfo);
                        
                        //diDelete.Delete(true);
                        //FileAttributes FA = diDelete.Attributes;
                        //if (FA == FileAttributes.Hidden)
                        //{
                        //}
                        
                        Console.WriteLine("Done...");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error deleting '" + diDelete.FullName + "' (" + ex.Message + ")");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unknown error in _deleteDirs (" + ex.Message + ")");
            }
        }

        private static void DeleteFileSystemInfo(FileSystemInfo fsi)
        {
            fsi.Attributes = FileAttributes.Normal;
            var di = fsi as DirectoryInfo;

            if (di != null)
            {
                foreach (var dirInfo in di.GetFileSystemInfos())
                    DeleteFileSystemInfo(dirInfo);
            }

            fsi.Delete();
        }
    }
}
